package neusoft.edu.cn.mysanjixiangmu527hdl.model;

import neusoft.edu.cn.mysanjixiangmu527hdl.bean.UserBean;
import neusoft.edu.cn.mysanjixiangmu527hdl.iface.LoginListener;
import neusoft.edu.cn.mysanjixiangmu527hdl.service.LoginService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Thinkpad on 2017/5/29.
 */

public class LoginModel {
    Retrofit retrofit;
    private static String BASEURL="amicool.neusoft.edu.cn";
    final static String PREFNAME="pref_login";
    public LoginModel() {
        retrofit=new Retrofit.Builder()
                              .baseUrl(BASEURL)
                              .addConverterFactory(GsonConverterFactory.create())
                              .build();
    }
    public void login(String userName, String password, final LoginListener listener){
        LoginService service=retrofit.create(LoginService.class);
        Call<UserBean> call=service.getLogin(userName,password);
        call.enqueue(new Callback<UserBean>() {
            @Override
            public void onResponse(Call<UserBean> call, Response<UserBean> response) {
                if(response.body()!=null&&response.isSuccessful()){
                    listener.onResponse(response.body());

                }
                else{
                    System.out.println("==>>"+"response.onFail()");
                    listener.onFail("onResponse fail");
                }

            }

            @Override
            public void onFailure(Call<UserBean> call, Throwable t) {
                    listener.onFail(toString());
            }
        });

    }
}
